﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace MuaBanLike.com
{
    public partial class MainWindow : Window
    {
        
        private bool checkFacebookLogin()
        {
            try
            {
                MainBrowser.Source = new Uri("https://www.facebook.com/");              
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return false;
        }

        BackgroundWorker threadFacebook = null;
        private void startThreadFacebook()
        {
            startTime = DateTime.Now;
            threadFacebook = new BackgroundWorker();

            threadFacebook.DoWork += delay1second;
            threadFacebook.RunWorkerCompleted += finishcheckfacbook;
            threadFacebook.WorkerReportsProgress = true;
            threadFacebook.WorkerSupportsCancellation = true;
            threadFacebook.RunWorkerAsync();
        }

        private void finishcheckfacbook(Object sender, RunWorkerCompletedEventArgs e)
        {
            //privacyFlyoutLabel is login
            if (threadFacebook != null && !threadFacebook.IsBusy)
            {
                threadFacebook.CancelAsync();
                threadFacebook = null;
                var doc = MainBrowser.Document as mshtml.HTMLDocument;
                if (doc != null)
                {
                    mshtml.HTMLAnchorElement div = doc.getElementById("navAccountLink") as mshtml.HTMLAnchorElement;
                    mshtml.HTMLInputElement pass = doc.getElementById("pass") as mshtml.HTMLInputElement;
                    if (div != null)
                    {
                        if (m_bChageAccount)
                        {
                            div.click();
                            //Start look for logout
                            startLogoutThread();

                        }
                        else
                        {
                            //m_bFacebookLogin = true;
                            try
                            {
                                //If You Want to define a defualt URI then you can define like below...
                                MainBrowser.Source = m_mainWeb;
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show(ex.Message);
                            }
                        }
                    }
                    else
                    {
                        
                        startThreadFacebook();
                    }
                }
            }
        }

       

        private void startLogoutThread()
        {
            BackgroundWorker threadLogin = new BackgroundWorker();
            threadLogin.DoWork += delay1second;
            threadLogin.RunWorkerCompleted += startLogoutFacebook;
            threadLogin.WorkerReportsProgress = true;
            threadLogin.WorkerSupportsCancellation = true;
            threadLogin.RunWorkerAsync();
        }

        private void startLogoutFacebook(Object sender, RunWorkerCompletedEventArgs e)
        {
            //uiLinkButtonInput
            var doc = MainBrowser.Document as mshtml.HTMLDocument;
            if (doc != null)
            {               
                mshtml.HTMLFormElement logOutForm = doc.getElementById("logout_form") as mshtml.HTMLFormElement;
                if (logOutForm != null)
                {
                    foreach (var child in logOutForm.children)
                    {

                        if (child != null && child.GetType().ToString() == "mshtml.HTMLLabelElementClass")
                        {
                            mshtml.HTMLInputElement logOutItem = ((mshtml.HTMLLabelElement)child).firstChild as mshtml.HTMLInputElement;

                            if (logOutItem != null)
                            {
                                logOutItem.click();
                                m_bChageAccount = false;
                                startThreadFacebook();
                                return;
                            }
                        }
                    }
                }
            }

            startLogoutThread();
        }
    }
}
